$(function() {
	$.fn.tooltip = function(params) {
		var options = {
			className: 'tooltip'
		};
		return this.each(function() {
			$this = $(this);
			var text = $this.text();
			$this.text(''); //clean exists text
			$('<em/>', {text: text}).appendTo($this);
			$(this).addClass(options.className);
		})
	}
	$('.container .content ol li a.button').tooltip();
	$.fn.maxlength = function(params) {
		return this.each(function() {
			var max = $(this).attr('maxlength');
			$(this).after($('<em/>', {text: 'Limit '+ max +' characters.'}));
		})
	}
	$('input[maxlength]').maxlength();
})

$(function() {
		   
		   
	var p = window.location.pathname ? window.location.pathname.split('/') : ['','/'];	
	$('.header .content .nav li a[href^=/'+ p[1] +']').first().addClass('active');
	$('.content input:checkbox[class=checkall]').click(function() {
		var checked = $(this).attr('checked');
		$('.content input:checkbox').attr({'checked': checked});
	});


	//delete
	$('a.delete').click(function() {
		if (!confirm('delete')) return false;
		var p = $(this).parent().parent();
		$.post($(this).attr('href'), function() {
			p.remove();
		})
		return false;
	});
	
	$('.container form input.delete').click(function() {
		if (!confirm('delete')) return false;

		$.post('delete/', $('.container form').serialize(), function() {
			$('input:checkbox:not(.checkall):checked').parent().parent().parent().remove();
		});

		return false;
	});

	
	$('.container .content').after($('<h3/>'));
	$(window).scroll(function() {
		if($('.footer').offset().top > $(window).height()) {
			$('.footer').css({'position': 'relative'});
		}
	});
	
	$('.container form input.deactive').click(function() {
		if (!confirm('Dectivate?')) return false;

		$.post('deactive', $('.container form').serialize(), function() {
			location.reload('/');
		});

		return false;
	});
	
	$('.container form input.active').click(function() {
		if (!confirm('Activate?')) return false;

		$.post('active', $('.container form').serialize(), function() {
			location.reload('/');
		});

		return false;
	});
	
	$('.container form input.deleted').click(function() {
		if (!confirm('Activate?')) return false;

		$.post('delete', $('.container form').serialize(), function() {
			location.reload('/');
		});

		return false;
	});
	
});